#---------------------------------------------------------------------------------#
### information: 
#---------------------------------------------------------------------------------#

### author: bjoern bremer (bjoern.bremer@eui.eu)
### last updated: 13 June 2017
### note: 
# this script descriptively analyses the data for the paper "the missing left? economic crisis and the response of social democratic parties in europe"
# tables are saved as .tex files, figure are saved as .pdf files

#---------------------------------------------------------------------------------#
### clear memory and define working directories
#---------------------------------------------------------------------------------#

rm(list = ls())

## remove the # and specify a directory in order to set a working directory
#setwd("")

#--------------------------------------------------------
### load the required libraries ### 
#--------------------------------------------------------

## remove the # in order to run the install.packages line once
# install.packages('plyr', 'doBy', 'ggplot2', 'reshape', 'MASS', 'nlme', 'grid', 'stargazer', 'xtable', 'data.table')

library(plyr)
library(doBy)
library(ggplot2)
library(reshape)
library(MASS)
library(nlme)
library(grid)
library(stargazer)
library(xtable)
library(data.table)

#--------------------------------------------------------
### load the data ###
#--------------------------------------------------------

## load the data (which should be saved in your working directory)
sum_all <- read.csv("missing_left_data.csv", stringsAsFactors=FALSE)

#--------------------------------------------------------
### create weighted mean of economic position ###
#--------------------------------------------------------

## calculate the left-right index as a weighted mean of the positions on welfare, eco_lib and budget
for(i in 1:nrow(sum_all)){
      sum_all$wm[i] <- (((sum_all$mean_wel[i]*sum_all$N_wel[i]) +
                               (sum_all$mean_eco[i]*sum_all$N_eco[i]) + 
                               (sum_all$mean_budget[i]*sum_all$N_budget[i])) /
                              (sum_all$N_wel[i] + sum_all$N_eco[i] + sum_all$N_budget[i]))
}

#--------------------------------------------------------
### create a simple sumamry df: all issues ###
#--------------------------------------------------------

## limit it to one pre-crisis election and all crisis elections
sum_all_simple <- sum_all[which (sum_all$year >= 2004),]

## create a variable for pre and crisis elections
sum_all_simple$election_type <- ifelse(sum_all_simple$year >= 2009, 1, 
                                  ifelse(sum_all_simple$year == 2008, 99, 0))

## delete elections that were in 2008
sum_all_simple <- sum_all_simple[which (sum_all_simple$election_type != 99),]

## summary by party family in each country
sum_all_simple <- ddply(sum_all_simple, c("country", "year","elec", "election","election_type","party_family"), summarise,
                        salience_all = mean(prop_all, na.rm = TRUE),
                        wm_all = mean(wm, na.rm = TRUE),
                        mean_wel = mean(mean_wel, na.rm = TRUE),
                        N_wel = mean(N_wel, na.rm = TRUE),
                        mean_eco = mean(mean_eco, na.rm = TRUE),
                        N_eco = mean(N_eco, na.rm = TRUE),
                        mean_budget = mean(mean_budget, na.rm = TRUE),
                        N_budget = mean(N_budget, na.rm = TRUE)
)

## take pre- and crisis averages per party_family in each country
sum_all_simple <- ddply(sum_all_simple, c("country", "party_family", "election_type"), summarise,
                        salience_all = mean(salience_all, na.rm = TRUE),
                        wm_all = mean(wm_all, na.rm = TRUE),
                        mean_wel = mean(mean_wel, na.rm = TRUE),
                        N_wel = mean(N_wel, na.rm = TRUE),
                        mean_eco = mean(mean_eco, na.rm = TRUE),
                        N_eco = mean(N_eco, na.rm = TRUE),
                        mean_budget = mean(mean_budget, na.rm = TRUE),
                        N_budget = mean(N_budget, na.rm = TRUE)
)

## limit it to four party families: far right, right, left, far left
sum_all_simple <- sum_all_simple[which (sum_all_simple$party_family == 1 | sum_all_simple$party_family == 2 | sum_all_simple$party_family == 4 | sum_all_simple$party_family == 5),]

## rename party families
sum_all_simple$party_family <- ifelse(sum_all_simple$party_family == 1, "Far Right",
                                      ifelse(sum_all_simple$party_family == 2, "Mod Right",
                                             ifelse(sum_all_simple$party_family == 4, "Mod Left",
                                                    ifelse(sum_all_simple$party_family == 5, "Far Left", NA))))

## order party families
sum_all_simple$party_family <- factor(sum_all_simple$party_family, levels = c("Far Left", "Mod Left", "Mod Right", "Far Right"))

## exclude parties if they are only covered in one election
# greece: far right
# ireland: far left
# italy: far right and far left
# switzerland: far left 
# uk: far right

sum_all_simple <- sum_all_simple[!(sum_all_simple$country=='Greece' & sum_all_simple$party_family =='Far Right'),]
sum_all_simple <- sum_all_simple[!(sum_all_simple$country=='Ireland' & sum_all_simple$party_family =='Far Left'),]
sum_all_simple <- sum_all_simple[!(sum_all_simple$country=='Italy' & sum_all_simple$party_family =='Far Left'),]
sum_all_simple <- sum_all_simple[!(sum_all_simple$country=='Italy' & sum_all_simple$party_family =='Far Right'),]
sum_all_simple <- sum_all_simple[!(sum_all_simple$country=='Switzerland' & sum_all_simple$party_family =='Far Left'),]
sum_all_simple <- sum_all_simple[!(sum_all_simple$country=='UK' & sum_all_simple$party_family =='Far Right'),]

#--------------------------------------------------------
### plot salience (figure 1) ###
#--------------------------------------------------------

pdf('all_salience.pdf')
all_salience.pdf <-ggplot(data = sum_all_simple[which (sum_all_simple$party_family == "Mod Left" | sum_all_simple$party_family == "Mod Right"),],
                          aes(x = factor(party_family), y=salience_all, fill = factor(election_type))) +
      geom_bar(stat ="identity", position=position_dodge()) + 
      facet_wrap (~ country, ncol = 3) + 
      xlab("Party ") +
      ylab("Salience of Economic Issues") + 
      theme_bw() + 
      theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
      scale_fill_grey(name = "Election Type", limits = c(0, 1), labels = c("Pre-Crisis", "Crisis"))
all_salience.pdf
dev.off()
all_salience.pdf

#--------------------------------------------------------
### plot the lr-index (figure 3) ###
#--------------------------------------------------------

pdf('all_lr.pdf')
all_lr <- ggplot(data = sum_all_simple[which (sum_all_simple$party_family == "Mod Left" | sum_all_simple$party_family == "Mod Right"),], 
                 aes(x = factor(party_family), y=wm_all, colour = factor(election_type), shape = factor(election_type))) +
      geom_point(size = 2) + 
      facet_wrap (~ country, ncol = 3) + 
      coord_flip() + 
      xlab("Party ") +
      ylab("Left-Right Position") + 
      theme_bw() + 
      scale_colour_grey(name = "Election Type", limits = c(0, 1), labels = c("Pre-Crisis", "Crisis"), start = 0.1, end = 0.6) +
      scale_shape_discrete(name = "Election Type", limits = c(0, 1), labels = c("Pre-Crisis", "Crisis"))
all_lr
dev.off()
all_lr

#--------------------------------------------------------
### distinguish between the three economic issues ###
#--------------------------------------------------------

## reshape sum_all_simple into long format
sum_all_simple_long <- reshape(sum_all_simple, 
                               varying=c("mean_wel", "mean_eco", "mean_budget", "N_wel", "N_eco", "N_budget"),
                               idvar=c("country", "party_family", "election_type"), 
                               ids = c("wel", "eco", "budget"),  timevar = "issue_name", direction="long", new.row.names = 1:1000, sep="_")

## rename variable name
names(sum_all_simple_long)[names(sum_all_simple_long) == 'N'] <- 'salience'

## rename issue names
sum_all_simple_long$issue_name <- ifelse(sum_all_simple_long$issue_name == 'budget', 'Budget', 
                                      ifelse(sum_all_simple_long$issue_name == 'eco', 'Eco Lib',
                                             ifelse(sum_all_simple_long$issue_name == 'wel', 'Welfare', NA)))

## turn election_type into a factor
sum_all_simple_long$election_type <- as.factor(sum_all_simple_long$election_type)

#--------------------------------------------------------
### plot salience of ML by economic issue (figure 2) ###
#--------------------------------------------------------

pdf('ml_salience_issues.pdf')
ml_salience_issues <-ggplot(data = sum_all_simple_long[which (sum_all_simple_long$party_family == "Mod Left"),], aes(x = issue_name, y=salience, fill = factor(election_type))) +
      geom_bar(stat ="identity", position=position_dodge()) + 
      facet_wrap (~ country, ncol = 3) + 
      xlab("Issue Category") +
      ylab("Salience of Economic Issues") + 
      theme_bw() + 
      theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
      scale_fill_grey(name = "Election Type", limits = c(0, 1), labels = c("Pre-Crisis", "Crisis"))
ml_salience_issues
dev.off()
ml_salience_issues

#--------------------------------------------------------
### plot position of ML by economic issue (figure 4) ###
#--------------------------------------------------------

## moderate left parties
pdf('ml_lr_issues.pdf')
ml_lr_issues <- ggplot(data = sum_all_simple_long[which (sum_all_simple_long$party_family == "Mod Left"),], aes(x = issue_name, y=mean, colour = election_type, shape = factor(election_type))) +
      geom_point(size = 2) + 
      facet_wrap (~ country, ncol = 3) + 
      coord_flip() + 
      xlab("Issue Category") +
      ylab("Left-Right Position") + 
      theme_bw() + 
      scale_colour_grey(name = "Election Type", limits = c(0, 1), labels = c("Pre-Crisis", "Crisis"), start = 0.1, end = 0.6) + 
      scale_shape_discrete(name = "Election Type", limits = c(0, 1), labels = c("Pre-Crisis", "Crisis"))
ml_lr_issues
dev.off()
ml_lr_issues

#--------------------------------------------------------------------------------------
### plot position by eleciton (figure 10) ###
#--------------------------------------------------------------------------------------

## reshape sum_all into long format
sum_all_long <- reshape(sum_all, 
                          varying=c("mean_wel", "mean_eco", "mean_budget", "N_wel", "N_eco", "N_budget"),
                          idvar=c("country", "party_family", "election"), 
                          ids = c("wel", "eco", "budget"),  timevar = "issue_name", direction="long", new.row.names = 1:1000, sep="_")

## limit it to one pre-crisis election and all crisis elections
sum_all_long <- sum_all_long[which (sum_all_long$year >= 2004),]

## delete elections that were in 2008
sum_all_long <- sum_all_long[which (sum_all_long$year != 2008),]

## limit it to countries that had more than one-crisis election
sum_all_long <- sum_all_long[which (sum_all_long$country != "Austria" & sum_all_long$country != "France" & sum_all_long$country != "Italy" & sum_all_long$country != "Ireland"),]

## summary by party family
sum_all_long_simple <- ddply(sum_all_long, c("country", "year","issue_name", "elec", "election","party_family"), summarise,
                             salience = mean(N, na.rm = TRUE),
                             mean = mean(mean, na.rm = TRUE))

## take averages 
sum_all_long_simple$election <- ifelse(sum_all_long_simple$election == 4, 1,
                                       ifelse(sum_all_long_simple$election == 3, 2, 
                                              ifelse(sum_all_long_simple$election == 2, 3,
                                                     ifelse(sum_all_long_simple$election  == 1, 4,NA))))


sum_all_long_simple <- ddply(sum_all_long_simple, c("country","issue_name", "party_family", "election"), summarise,
                             salience = mean(salience, na.rm = TRUE),
                             mean = mean(mean, na.rm = TRUE))

## limit it to four party families: far right, right, left, far left
sum_all_long_simple <- sum_all_long_simple[which (sum_all_long_simple$party_family == 1 | sum_all_long_simple$party_family == 2 | sum_all_long_simple$party_family == 4 | sum_all_long_simple$party_family == 5),]

## rename party families
sum_all_long_simple$party_family <- ifelse(sum_all_long_simple$party_family == 1, "Far Right",
                                           ifelse(sum_all_long_simple$party_family == 2, "Mod Right",
                                                  ifelse(sum_all_long_simple$party_family == 4, "Mod Left",
                                                         ifelse(sum_all_long_simple$party_family == 5, "Far Left", NA))))

## order party families
sum_all_long_simple$party_family <- factor(sum_all_long_simple$party_family, levels = c("Far Left", "Mod Left", "Mod Right", "Far Right"))

## create variable for category
sum_all_long_simple$Category <- sum_all_long_simple$issue_name

## plot the graph (figure 10)
pdf('ml_lr_change_countries.pdf', width = 12, height = 12)
ml_lr_change_countries <-ggplot(data = sum_all_long_simple[which (sum_all_long_simple$party_family == "Mod Left"),], 
                                aes(x = election, y= mean, colour= Category)) +
      geom_line(aes(linetype = Category)) +
      geom_point(aes(shape = Category)) + 
      facet_wrap (~ country, ncol = 2) +
      xlab("Election Type") +
      ylab("Left-Right Position") + 
      theme_bw() +
      scale_color_grey(limits = c("eco", "wel", "budget"), labels = c("Economic Liberalism", "Welfare", "Budegtary Rigour"), start = 0,
                       end = 0.6) +
      scale_linetype_discrete(limits = c("eco", "wel", "budget"), labels = c("Economic Liberalism", "Welfare", "Budegtary Rigour")) +
      scale_shape_discrete(limits = c("eco", "wel", "budget"), labels = c("Economic Liberalism", "Welfare", "Budegtary Rigour")) +
      theme(legend.key.width = unit(1, "cm")) +
      scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c("Pre-Crisis", "Crisis I", "Crisis II", "Crisis III")) + 
      theme(axis.title.x=element_text(margin=margin(10,10,10,10))) + 
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
ml_lr_change_countries
dev.off()
ml_lr_change_countries

#--------------------------------------------------------------------------------------
### plot position by eleciton (figure 5) ###
#--------------------------------------------------------------------------------------

## take the average position across country by party family and election type
sum_all_long_average <- ddply(sum_all_long, c("issue_name", "party_family", "election"), summarise,
                                     salience = mean(N, na.rm = TRUE),
                                     mean = mean(mean, na.rm = TRUE)
)

## rename party families
sum_all_long_average$party_family <- ifelse(sum_all_long_average$party_family == 1, "Far Right",
                                           ifelse(sum_all_long_average$party_family == 2, "Mod Right",
                                                  ifelse(sum_all_long_average$party_family == 4, "Mod Left",
                                                         ifelse(sum_all_long_average$party_family == 5, "Far Left", NA))))

## order party families
sum_all_long_average$party_family <- factor(sum_all_long_average$party_family, levels = c("Far Left", "Mod Left", "Mod Right", "Far Right"))

## exclude third and fourt-crisis election
sum_all_long_average <- sum_all_long_average[which (sum_all_long_average$election != 4),]

## create variable for issue category
sum_all_long_average$Category <- sum_all_long_average$issue_name

## plot the graph (figure 5)
pdf('ml_lr_change_average.pdf', width = 6, height = 4)
ml_lr_change_average <-ggplot(data = sum_all_long_average[which (sum_all_long_average$party_family == "Mod Left"),], 
                                  aes(x = election, y= mean, colour= Category)) +
      geom_line(aes(linetype = Category)) +
      geom_point(aes(shape = Category)) + 
      xlab("Election Type") +
      ylab("Left-Right Position") + 
      theme_bw() +  
      scale_color_grey(limits = c("eco", "wel", "budget"), labels = c("Economic Liberalism", "Welfare", "Budegtary Rigour"), start = 0,
                       end = 0.6) +
      scale_linetype_discrete(limits = c("eco", "wel", "budget"), labels = c("Economic Liberalism", "Welfare", "Budegtary Rigour")) +
      scale_shape_discrete(limits = c("eco", "wel", "budget"), labels = c("Economic Liberalism", "Welfare", "Budegtary Rigour")) +
      theme(legend.key.width = unit(1, "cm")) +
      scale_x_continuous(breaks = c(1, 2, 3), labels = c("Pre-Crisis", "Crisis I", "Crisis II")) + 
      scale_y_continuous(breaks = c(-0.6, -0.4, -0.2, 0, 0.2, 0.4), limits = c(-0.7, 0.4)) + 
      theme(axis.title.x=element_text(margin=margin(10,10,10,10)))
ml_lr_change_average
dev.off()
ml_lr_change_average

#-----------------------------------------------------------------------------------
### calculate nominal change of salience and wm of economic position (table 10) ###
#-----------------------------------------------------------------------------------

## reshape dataframe
sum_all_simple_wide <- reshape(sum_all_simple, 
                timevar = "election_type",
                idvar = c("country", "party_family"),
                direction = "wide")

## calculate change per party family in a given country
sum_all_simple_wide$salience_change <- (sum_all_simple_wide$salience_all.1 - sum_all_simple_wide$salience_all.0)
sum_all_simple_wide$mean_change <- (sum_all_simple_wide$wm_all.1 - sum_all_simple_wide$wm_all.0)

## take the average across countries
wide_average <- ddply(sum_all_simple_wide, c("party_family"), summarise, 
                     salience_all.0 = mean(salience_all.0, na.rm=TRUE), 
                     mean_all.0 = mean(wm_all.0, na.rm=TRUE), 
                     salience_all.1 = mean(salience_all.1, na.rm=TRUE), 
                     mean_all.1 = mean(wm_all.1, na.rm=TRUE)) 

wide_average$salience_change <- (wide_average$salience_all.1 - wide_average$salience_all.0)
wide_average$mean_change <- (wide_average$mean_all.1 - wide_average$mean_all.0)

## table 10: combine both salience and wm in one table
wide_average <- subset(wide_average, select = c("party_family","salience_all.0", "salience_all.1", "salience_change", "mean_all.0", "mean_all.1", "mean_change"))
wide_average <- rename(wide_average, c("party_family"="Party Family", "salience_all.0" = "Pre-Crisis", "salience_all.1" = "Crisis", "salience_change"="Change", "mean_all.0" = "Pre-Crisis", "mean_all.1" = "Crisis", "mean_change"="Change"))
print(xtable(wide_average, digits = 2, auto = TRUE), include.rownames=FALSE, type="latex", file="all_change.tex")

#--------------------------------------------------------------------------------------
### calculate nominal change of salience and mean for ind. issue area (table 11-13) ###
#--------------------------------------------------------------------------------------

## calculate change inof salience and mean for ind. issue area
sum_all_simple_wide$N_wel_change <- (sum_all_simple_wide$N_wel.1 - sum_all_simple_wide$N_wel.0)
sum_all_simple_wide$mean_wel_change <- (sum_all_simple_wide$mean_wel.1 - sum_all_simple_wide$mean_wel.0)
sum_all_simple_wide$N_eco_change <- (sum_all_simple_wide$N_eco.1 - sum_all_simple_wide$N_eco.0)
sum_all_simple_wide$mean_eco_change <- (sum_all_simple_wide$mean_eco.1 - sum_all_simple_wide$mean_eco.0)
sum_all_simple_wide$N_budget_change <- (sum_all_simple_wide$N_budget.1 - sum_all_simple_wide$N_budget.0)
sum_all_simple_wide$mean_budget_change <- (sum_all_simple_wide$mean_budget.1 - sum_all_simple_wide$mean_budget.0)

## limit it to the moderate left
wide_ml <- sum_all_simple_wide[which (sum_all_simple_wide$party_family == "Mod Left"),]

## table 11: change of ml parties on welfare
wide_ml_wel <- subset(wide_ml , select = c("country","N_wel.0", "N_wel.1", "N_wel_change", "mean_wel.0", "mean_wel.1", "mean_wel_change"))
wide_ml_wel <- rename(wide_ml_wel, c("country"="Country", "N_wel.0"="Pre-Crisis", "N_wel.1"="Crisis", "N_wel_change"="Change", "mean_wel.0"="Pre-Crisis", "mean_wel.1"="Crisis", "mean_wel_change"="Change"))
print(xtable(wide_ml_wel, digits = 2, auto = TRUE), include.rownames=FALSE, type="latex", file="ml_change_wel.tex")

## table 12: change of ml parties on economic liberalism
wide_ml_eco <- subset(wide_ml , select = c("country","N_eco.0", "N_eco.1", "N_eco_change", "mean_eco.0", "mean_eco.1", "mean_eco_change")) 
wide_ml_eco <- rename(wide_ml_eco, c("country"="Country", "N_eco.0"="Pre-Crisis", "N_eco.1"="Crisis", "N_eco_change"="Change", "mean_eco.0"="Pre-Crisis", "mean_eco.1"="Crisis", "mean_eco_change"="Change"))
print(xtable(wide_ml_eco, digits = 2, auto = TRUE), include.rownames=FALSE, type="latex", file="ml_change_eco.tex")

## table 13: change of ml parties on budget
wide_ml_budget <- subset(wide_ml , select = c("country","N_budget.0", "N_budget.1", "N_budget_change", "mean_budget.0", "mean_budget.1", "mean_budget_change")) 
wide_ml_budget <- rename(wide_ml_budget, c("country"="Country", "N_budget.0"="Pre-Crisis", "N_budget.1"="Crisis", "N_budget_change"="Change", "mean_budget.0"="Pre-Crisis", "mean_budget.1"="Crisis", "mean_budget_change"="Change"))
print(xtable(wide_ml_budget, digits = 2, auto = TRUE), include.rownames=FALSE, type="latex", file="ml_change_budget.tex")